package controllers;

import Dao.DBConnect;
import application.Main;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import models.LoginModel;
import models.OrderModel;
import models.ShopModel;

import java.net.URL;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class LoginController implements Initializable {

	@FXML
	private ChoiceBox cb_s_01;
	@FXML
	private TextField txtUsername;

	@FXML
	private PasswordField txtPassword;

	@FXML
	private Label lblError;

	private LoginModel model;
	ShopModel sp = null;

	public LoginController() {


		model = new LoginModel();

	}

	public void login() {

		lblError.setText("");
		String username = this.txtUsername.getText();
		String password = this.txtPassword.getText();

		// Validations
		if (username == null || username.trim().equals("")) {
			lblError.setText("Username Cannot be empty or spaces");
			return;
		}
		if (password == null || password.trim().equals("")) {
			lblError.setText("Password Cannot be empty or spaces");
			return;
		}
		if (username == null || username.trim().equals("") && (password == null || password.trim().equals(""))) {
			lblError.setText("User name / Password Cannot be empty or spaces");
			return;
		}

		// authentication check
		checkCredentials(username, password);

	}

	public void checkCredentials(String username, String password) {
		Boolean isValid = model.getCredentials(username, password);
		if (!isValid) {
			lblError.setText("Password incorrect or User does not exist!");
			return;
		}
		try {
			AnchorPane root;
			if (model.isAdmin() && isValid) {
				// If user is admin, inflate admin view

				root = (AnchorPane) FXMLLoader.load(getClass().getResource("/views/AdminView.fxml"));
				Main.stage.setTitle("Admin View");

			}
			else if (model.isSale() && isValid) {

				root = (AnchorPane) FXMLLoader.load(getClass().getResource("/views/SaleView.fxml"));
//				SalesController.
				Main.stage.setTitle("Sales View");



			}
			else {
				// If user is customer, inflate customer view

				root = (AnchorPane) FXMLLoader.load(getClass().getResource("/views/CustomerView.fxml"));
				// ***Set user ID acquired from db****
				int user_id = model.getId();
				String user_name = model.getUname();
				CustomerController.setUserid(user_id);
				CustomerController.setUname(user_name);
				CustomerController.setSid(Integer.parseInt(cb_s_01.getValue().toString()));
				Main.stage.setTitle("Client View");
			}

			Scene scene = new Scene(root);
			Main.stage.setScene(scene);

		} catch (Exception e) {
			System.out.println("Error occured while inflating view: " + e);
		}

	}

	@Override
	public void initialize(URL location, ResourceBundle resources) {

//		model = new LoginModel();
		DBConnect conn = new DBConnect();
		List<ShopModel> sps = new ArrayList<>();
		List<String> ls = new ArrayList<>();
		sp = new ShopModel();
		sps = sp.getAll();
		System.out.println(sps.toString());
		for (ShopModel str : sps) {

			ls.add(String.valueOf(str.getSid()));

		}
		System.out.println(ls.toString());
		cb_s_01.getItems().addAll(ls);
		cb_s_01.setValue("1");
	}
}